package com.javastudy.forum.controller;

import com.javastudy.forum.common.AppResult;
import com.javastudy.forum.common.ResultCode;
import com.javastudy.forum.config.AppConfig;
import com.javastudy.forum.model.Article;
import com.javastudy.forum.model.User;
import com.javastudy.forum.service.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Chooker
 * @create 2023-08-17 23:06
 */
@RestController
@Slf4j
@RequestMapping("/article")
@Api(tags = "文章接口")
public class ArticleController {
    @Resource
    private IArticleService articleService;

    @GetMapping("/getAllByBoardId")
    @ApiOperation("获得所有的文章")
    public AppResult<List<Article>> getAllByBoardId(@ApiParam("板块id")
                                                    @RequestParam(value = "boardId", required = false) Long boardId) {
        List<Article> articles;
        if (boardId == null) {
            articles = articleService.selectAll();
        } else {
            articles = articleService.selectByBoardId(boardId);
        }
        if (articles == null) {
            articles = new ArrayList<>();
        }
        return AppResult.success(articles);

    }

    @ApiOperation("发布文章")
    @PostMapping("/create")
    public AppResult create(@ApiParam("文章标题") @RequestParam("title") @NonNull String title,
                            @ApiParam("文章内容") @RequestParam("content") @NonNull String content,
                            @ApiParam("板块id") @RequestParam("boardId") @NonNull Long boardId,
                            HttpServletRequest httpServletRequest) {
        //获取用户id
        HttpSession session = httpServletRequest.getSession(false);
        User user = (User) session.getAttribute(AppConfig.SESSION_USER_KEY);
        // 校验用户状态
        if (user.getState() == 1) {
            // 用户已禁言, 返回提示
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //设置文章信息
        Article article = new Article();
        article.setUserId(user.getId());
        article.setTitle(title);
        article.setBoardId(boardId);
        article.setContent(content);

        articleService.create(article);

        return AppResult.success("新增文章成功");

    }

    @ApiOperation("通过文章id获取文章的详细信息")
    @GetMapping("/getById")
    public AppResult getById(@ApiParam("文章id") @RequestParam("id") @NonNull Long id,
                             HttpServletRequest httpServletRequest) {
        Article article = articleService.selectById(id);
        if (article == null) {
            // 返回提示
            return AppResult.failed("帖子不存在");
        }
        articleService.updateVisitCountById(id);
        article.setVisitCount(article.getVisitCount() + 1);
        HttpSession session = httpServletRequest.getSession(false);
        User user = (User) session.getAttribute(AppConfig.SESSION_USER_KEY);
        if (user.getId() == article.getUserId()) {
            article.setOwn(true);
        }
        return AppResult.success(article);

    }

    @ApiOperation("更新文章")
    @PostMapping("/modify")
    public AppResult modify(@ApiParam("文章标题") @RequestParam("title") @NonNull String title,
                            @ApiParam("文章内容") @RequestParam("content") @NonNull String content,
                            @ApiParam("文章id") @RequestParam("id") @NonNull Long id,
                            HttpServletRequest httpServletRequest) {
        // 从session中获取当前登录的用户
        HttpSession session = httpServletRequest.getSession(false);
        User user = (User) session.getAttribute(AppConfig.SESSION_USER_KEY);
        // 校验用户信息
        if (user.getState() == 1) {
            // 返回提示信息
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        // 查询帖子信息
        Article article = articleService.selectById(id);
        // 校验帖子是否存在
        if (article == null) {
            // 返回提示信息
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 判断当前登录用户是不是作者
        if (article.getUserId() != user.getId()) {
            // 返回提示信息
            return AppResult.failed("您不是帖子的作者，无权修改.");
        }
        // 校验帖子状态
        if (article.getState() == 1) {
            // 返回提示信息
            return AppResult.failed("帖子状态异常, 请联系管理员");
        }
        articleService.modify(id, title, content);
        return AppResult.success("更新成功");

    }

    @ApiOperation("删除帖子")
    @PostMapping("/delete")
    public AppResult delete(@ApiParam("文章id") @RequestParam("id") @NonNull Long id,
                            HttpServletRequest httpServletRequest) {
        // 从session中获取当前登录的用户
        HttpSession session = httpServletRequest.getSession(false);
        User user = (User) session.getAttribute(AppConfig.SESSION_USER_KEY);
        // 校验用户信息
        if (user.getState() == 1) {
            // 返回提示信息
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        // 查询帖子信息
        Article article = articleService.selectById(id);
        // 校验帖子是否存在
        if (article == null) {
            // 返回提示信息
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 判断当前登录用户是不是作者
        if (article.getUserId() != user.getId()) {
            // 返回提示信息
            return AppResult.failed("您不是帖子的作者，无权修改.");
        }
        // 校验帖子状态
        if (article.getState() == 1) {
            // 返回提示信息
            return AppResult.failed("帖子状态异常, 请联系管理员");
        }
        articleService.delete(id);
        return AppResult.success("删除成功");

    }

    @ApiOperation("点赞帖子")
    @PostMapping("/thumbsUp")
    public AppResult thumbsUp(@ApiParam("文章id") @RequestParam("id") @NonNull Long id,
                              HttpServletRequest httpServletRequest) {
        // 从session中获取当前登录的用户
        HttpSession session = httpServletRequest.getSession(false);
        User user = (User) session.getAttribute(AppConfig.SESSION_USER_KEY);
        // 校验用户信息
        if (user.getState() == 1) {
            // 返回提示信息
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        // 查询帖子信息
        Article article = articleService.selectById(id);
        // 校验帖子是否存在
        if (article == null) {
            // 返回提示信息
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 判断当前登录用户是不是作者
        if (article.getUserId() != user.getId()) {
            // 返回提示信息
            return AppResult.failed("您不是帖子的作者，无权修改.");
        }
        // 校验帖子状态
        if (article.getState() == 1) {
            // 返回提示信息
            return AppResult.failed("帖子状态异常, 请联系管理员");
        }
        articleService.thumbsUpById(id);
        return AppResult.success("点赞成功");

    }

    @ApiOperation("获取用户文章列表")
    @GetMapping("/getAllByUserId")
    public AppResult<List<Article>> getAllByUserId(@ApiParam("用户id") @RequestParam(value = "userId", required = false) Long userId,
                                                   HttpServletRequest httpServletRequest) {
        if (userId == null) {
            HttpSession session = httpServletRequest.getSession(false);
            User user = (User) session.getAttribute(AppConfig.SESSION_USER_KEY);
            userId = user.getId();
        }
        List<Article> articles = articleService.selectByUserId(userId);
        if (articles == null) {
            articles = new ArrayList<>();
        }
        return AppResult.success(articles);
    }


}
